﻿/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import envisage.Data.IBool;
	import envisage.Data.UIBoolEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	
	/*
	UI class representing a simple button control, normal or toggle.
	*/
	public class Button extends Control
	{	
		public function Button()
		{
			super();
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown);
		}
		
		private var dataBool:IBool;
		public function get DataBool():IBool { return dataBool; }
		public function set DataBool(iBool:IBool):void
		{
			if( dataBool != null ) dataBool.removeEventListener(UIBoolEvent.UPDATED, OnBoolUpdated);
			dataBool = iBool;
			if( dataBool != null ) dataBool.addEventListener(UIBoolEvent.UPDATED, OnBoolUpdated);
		}
		
		public function OnBoolUpdated(e:UIBoolEvent):void
		{
			SetDown(dataBool.GetValue());
		}
		
		private var autoRepeat:Boolean = false;
		private var toggleMode:Boolean = false;		
		
		private var down:Boolean = false;
		private var selected:Boolean = false;
		
		private var radioGroup:Array = null;
		
		protected function Down():void
		{
			var f:Function = this["down"] as Function;
			if( f != null ) f(down);
		}

		public function StandardInterface():void
		{
			useHandCursor = true;
			buttonMode 	  = true;
			mouseChildren = false;
		}
		
		protected function GetDown():Boolean { return down; }
		
		private function SetDown(d:Boolean):void
		{
			if( down != d )
			{
				down = d;
				Down();
			}
		}
		
		private function SetSelected(s:Boolean):void
		{
			if( selected != s )
			{
				selected = s;
				//Selected();
				dispatchEvent(new EnvisageEvent(selected? EnvisageEvent.SELECTED : EnvisageEvent.DESELECTED, null, this, null));
			}
		}
		
		public function GetSelected():Boolean
		{
			return selected;
		}
		
		public function Click():void
		{
			PositionDown();
			PositionUp(true);
		}
		
		private function PositionDown():void
		{
			if( toggleMode )
			{
				//If toggle mode, swap the current state.
				SetDown(!down);
				SetSelected(!GetSelected());
				if( dataBool != null ) dataBool.SetValue(GetSelected());
				return;
			}
			
			if( radioGroup != null )
			{
				for(var i:int = 0 ; i < radioGroup.length ; i++)
				{
					Button(radioGroup[i]).SetDown(false);
					Button(radioGroup[i]).SetSelected(false);
				}
				
				SetDown(true);
				SetSelected(true);
				return;
			}
			
			//If push mode, wait for the mouse to come back up over the button.
			SetDown(true);
		}
		
		//Called by the framework 
		private function OnMouseDown(e:MouseEvent):void
		{
			PositionDown();
			
			this.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			
			if( autoRepeat )
			{
				this.stage.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
				repCounter = 0;
			}
		}
		
		private var repSecs:Number = 0.1;
		private var repCounter:Number = 0;
		//Called by the framework, for an auto-repeat button only.
		private function OnEnterFrame(e:Event):void
		{
			repCounter -= 1.0 / Number(stage.frameRate);
			
			if( repCounter < 0 ) 
			{
				dispatchEvent(new EnvisageEvent(EnvisageEvent.SELECTED, null, this, null));
				repCounter = repSecs;
			}
		}
		
		private function PositionUp(over:Boolean):void
		{
			
		
			if( toggleMode == false && radioGroup == null )
			{
				SetDown(false);
				//If not in toggle mode, and mouse is up over button, dispatch click event.
				if( over && autoRepeat == false )
					dispatchEvent(new EnvisageEvent(EnvisageEvent.SELECTED, null, this, null)); 
			}
		}
		
		//Called by the framework.
		private function OnMouseUp(e:MouseEvent):void
		{
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			
			var over:Boolean = this.hitTestPoint(e.stageX, e.stageY, false);
			
			PositionUp(over);
			
			if( autoRepeat )
				this.stage.removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
		}
		
		//Sets the button to toggle or push mode.
		public function SetToggleMode(m:Boolean):void
		{
			this.toggleMode = m;
		}
		
		//Returns whether the button is in toggle or push mode.
		public function GetToggleMode():Boolean
		{
			return toggleMode;
		}
		
		//Sets the button to auto-repeat mode.
		public function SetAutoRepeat(ar:Boolean):void
		{
			this.autoRepeat = ar;
		}
		
		
		public function SetRadioGroup(arrButtons:Array):void
		{
			radioGroup = arrButtons;
		}
	
	}
}


